package com.example.choupaiapp.common.util;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.choupaiapp.R;

public class ToastUtil {
    private static Toast mShorToast;
    public static void showToast(String message){
        if(mShorToast==null){
            mShorToast = Toast.makeText(ContextUtil.getContext(),message,Toast.LENGTH_SHORT);
        }else{
            mShorToast.setText(message);
        }
        mShorToast.show();
    }

    private static Toast mLongToast;
    public static void showLongToast(String message){
        if(mLongToast==null){
            mLongToast = Toast.makeText(ContextUtil.getContext(),message,Toast.LENGTH_LONG);
        }else{
            mLongToast.setText(message);
        }
        mLongToast.show();
    }

    public static void showImageToas(String message) {
        View toastview = LayoutInflater.from(ContextUtil.getContext()).inflate(R.layout.toast_image_layout, null);
        TextView tipTextView = (TextView) toastview.findViewById(R.id.tipTextView);
        tipTextView.setText(message);    //要提示的文本
        Toast toast = new Toast(ContextUtil.getContext());   //上下文
        toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);   //位置居中
        toast.setDuration(Toast.LENGTH_SHORT);  //设置短暂提示
        toast.setView(toastview);   //把定义好的View布局设置到Toast里面
        toast.show();

        toastview.setOnClickListener(view -> toast.cancel());
    }
}